//
//  HomeViewController.m
//  geiTA
//
//  Created by 曲丹阳 on 14-11-19.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "HomeViewController.h"
#import "HomemyViewCell.h"
#import "Homeview.h"
#import "Homekindview.h"
#import "HomeKindViewController.h"
#import "HomeModel.h"
#import "TitleXQViewController.h"
#import "UIButton+WebCache.h"

@interface HomeViewController ()<UIScrollViewAccessibilityDelegate>
@property(nonatomic, retain)NSMutableArray *news;
@property(nonatomic, retain)NSTimer *timer;
@property (nonatomic, retain)  UIPageControl *page;
@end

@implementation HomeViewController
- (void)dealloc

{
    [_button release];
    [_button1 release];
    [_button2 release];
    [_button3 release];
    [_button4 release];


    [_news release];
    [super dealloc];
}
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.news = [NSMutableArray array];
  
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent =YES;

    [self creatView];
    [self creatjson];
    [self creatHomeview];
    [self creatHomekindview];
    
}


-(void)creatView
{
    self.scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 5)];
    self.scroll.delegate = self;
    [_scroll setContentSize:CGSizeMake(_scroll.frame.size.width * 5 , 0)];
    [_scroll setBackgroundColor:[UIColor purpleColor]];
    
    self.button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, _scroll.frame.size.width, _scroll.frame.size.height)];
    [_button setBackgroundColor:[UIColor blackColor]];
       self.button1 = [[UIButton alloc]initWithFrame:CGRectMake(_scroll.frame.size.width * 1, 0, _scroll.frame.size.width, _scroll.frame.size.height)];
    [_button1 setBackgroundColor:[UIColor redColor]];
       self.button2 = [[UIButton alloc]initWithFrame:CGRectMake(_scroll.frame.size.width * 2,0 , _scroll.frame.size.width, _scroll.frame.size.height)];
      self.button3 = [[UIButton alloc]initWithFrame:CGRectMake( _scroll.frame.size.width * 3,0, _scroll.frame.size.width, _scroll.frame.size.height)];
       self.button4 = [[UIButton alloc]initWithFrame:CGRectMake(_scroll.frame.size.width * 4,0 , _scroll.frame.size.width, _scroll.frame.size.height)];
    
   
    
    [_scroll addSubview:_button];
    [_scroll addSubview:_button1];
    [_scroll addSubview:_button2];
    [_scroll addSubview:_button3];
    [_scroll addSubview:_button4];
    [_scroll setShowsHorizontalScrollIndicator:YES];
    [_scroll setPagingEnabled:YES];
    [_scroll setUserInteractionEnabled:YES];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(scollTimer) userInfo:nil repeats:YES];
    
    [self.view addSubview:_scroll];
    self.page = [[UIPageControl alloc]initWithFrame:CGRectMake(self.scroll.frame.size.width / 1.5, self.scroll.frame.size.height - 20, 80, 20)];
    self.page.currentPage = 0;
    [self.page setNumberOfPages:5];
    [_page addTarget:self action:@selector(button:) forControlEvents:UIControlEventValueChanged];
 
    [self.view addSubview:_page];
    

    [_button release];
        [_button1 release];
        [_button2 release];
        [_button3 release];
        [_button4 release];
    
    
    
}

-(void)button : (UIPageControl *)page
{
    [_scroll setContentOffset:CGPointMake(_scroll.frame.size.width * page.currentPage, 0)];
}


-(void)scollTimer
{
    CGPoint p = _scroll.contentOffset;
    [_scroll setContentOffset:CGPointMake(self.view.frame.size.width + p.x, 0)];
    if (p.x >= self.view.frame.size.width * 4) {
        _scroll.contentOffset = CGPointMake(0, 0);
    }
}


-(void)changvalue
{
   
    
    HomeModel *model = [[HomeModel alloc]init];
    model = [self.news objectAtIndex:0];
    [self.button addTarget:self action:@selector(buttonback:) forControlEvents:UIControlEventTouchUpInside];
    [self.button setImageWithURL:[NSURL URLWithString:model.cover_image_url]];
    HomeModel *model1 = [[HomeModel alloc]init];
    model1 = [_news objectAtIndex:1];
    [self.button1 setImageWithURL:[NSURL URLWithString:model1.cover_image_url]];
    [self.button1 addTarget:self action:@selector(buttonback1:) forControlEvents:UIControlEventTouchUpInside];
    HomeModel *model2 = [[HomeModel alloc]init];
    model2 = [_news objectAtIndex:2];
    [self.button2 setImageWithURL:[NSURL URLWithString:model2.cover_image_url]];
    [self.button2 addTarget:self action:@selector(buttonback2:) forControlEvents:UIControlEventTouchUpInside];
    HomeModel *model3 = [[HomeModel alloc]init];
    model3 = [_news objectAtIndex:3];
    [self.button3 addTarget:self action:@selector(buttonback3:) forControlEvents:UIControlEventTouchUpInside];
    [self.button3 setImageWithURL:[NSURL URLWithString:model3.cover_image_url]];
    HomeModel *model4 = [[HomeModel alloc]init];
    model4 = [_news objectAtIndex:4];
    [self.button4 addTarget:self action:@selector(buttonback4:) forControlEvents:UIControlEventTouchUpInside];
    [self.button4 setImageWithURL:[NSURL URLWithString:model4.cover_image_url]];
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, self.button.frame.size.height - 40, self.button.frame.size.width - 20, 40)];
    UILabel *lable1 = [[UILabel alloc]initWithFrame:CGRectMake(0, self.button.frame.size.height - 40, self.button.frame.size.width - 20, 40)];
      UILabel *lable2 = [[UILabel alloc]initWithFrame:CGRectMake(0, self.button.frame.size.height - 40, self.button.frame.size.width - 20, 40)];
      UILabel *lable3 = [[UILabel alloc]initWithFrame:CGRectMake(0, self.button.frame.size.height - 40, self.button.frame.size.width - 20, 40)];
    UILabel *lable4 = [[UILabel alloc]initWithFrame:CGRectMake(0, self.button.frame.size.height - 40, self.button.frame.size.width - 20, 40)];
    [self.button addSubview:lable];
     [self.button1 addSubview:lable1];
     [self.button2 addSubview:lable2];
     [self.button3 addSubview:lable3];
     [self.button4 addSubview:lable4];
    [lable setText:model.title];
    [lable1 setText:model1.title];
    [lable2 setText:model2.title];
    [lable3 setText:model3.title];
    [lable4 setText:model4.title];
    
    
}
-(void)buttonback :(id)sender
{
    TitleXQViewController *title = [[TitleXQViewController alloc]init];
    HomeModel *model = [self.news objectAtIndex:0];
    [title changurl:model.content_url];
    [self.navigationController pushViewController:title animated:YES];
}
-(void)buttonback1 :(id)sender
{
    TitleXQViewController *title = [[TitleXQViewController alloc]init];
    HomeModel *model = [self.news objectAtIndex:1];
    [title changurl:model.content_url];
   [self.navigationController pushViewController:title animated:YES];
}
-(void)buttonback2 :(id)sender
{
    TitleXQViewController *title = [[TitleXQViewController alloc]init];
    HomeModel *model = [self.news objectAtIndex:2];
    [title changurl:model.content_url];
    [self.navigationController pushViewController:title animated:YES];
}
-(void)buttonback3 :(id)sender
{
    TitleXQViewController *title = [[TitleXQViewController alloc]init];
    HomeModel *model = [self.news objectAtIndex:3];
    [title changurl:model.content_url];
    [self.navigationController pushViewController:title animated:YES];
    
}
-(void)buttonback4 :(id)sender
{
    TitleXQViewController *title = [[TitleXQViewController alloc]init];
    HomeModel *model = [self.news objectAtIndex:4];
    [title changurl:model.content_url];
    [self.navigationController pushViewController:title animated:YES];
}



-(void)creatjson
{
    NSString *str = [NSString stringWithFormat:@"http://api.liwushuo.com/v1/channels/1/items?limit=5&offset=%u", arc4random()%320];
    NSURL *url = [NSURL URLWithString:str];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableDictionary *dic1 = [dic objectForKey:@"data"];
        NSMutableArray *arr = [dic1 objectForKey:@"items"];
        for (NSMutableDictionary *dic in arr) {
            HomeModel *model = [[HomeModel alloc]init];
            [model  setValuesForKeysWithDictionary:dic];
            [self.news addObject:model];
        }
        [self changvalue];
    }];
    
}
-(void)removeNSTimer
{
    [self.timer invalidate];
    self.timer = nil;
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    
   self.timer =[NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(scollTimer) userInfo:nil repeats:YES];

}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self removeNSTimer];
}



-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == _scroll) {
        
        int a = _scroll.contentOffset.x / _scroll.frame.size.width ;
        [self.page  setCurrentPage:a];
        if (scrollView.contentOffset.x < 0) {
            [_scroll setContentOffset:CGPointMake(_scroll.frame.size.width * 4 , 0)];

        }
         else if (scrollView.contentOffset.x > _scroll.frame.size.width * 5) {
            [_scroll setContentOffset:CGPointMake(_scroll.frame.size.width  , 0)];
        }
    }
    
    
}

-(BOOL)scrollViewShouldScrollToTop:(UIScrollView*)scrollView
{
    return YES;
}

-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.tabBarController.tabBar.hidden = NO;
       self.navigationController.navigationBar.translucent =NO;

}


#pragma mark-
#pragma mark DStimer

-(void)creatHomeview
{
   self.homeview = [[Homeview alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height / 5 + 5, self.view.frame.size.width, self.view.frame.size.height / 6)];
    [_homeview setBackgroundColor:[UIColor lightGrayColor]];
     [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFire:) userInfo:self repeats:YES];
    
   
    
 
    [self.view addSubview:_homeview];
    [_homeview release];
}


-(void)timerFire :(NSTimer *)theTimer
{
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *endtime = [[NSDateComponents alloc]init];
    [endtime setYear:2016];
    [endtime setMonth:10];
    [endtime setDay:10];
    [endtime setHour:10];
    [endtime setMinute:10];
    [endtime setSecond:10];
    NSDate *today = [cal dateFromComponents:endtime];
    [endtime release];
    NSDate *oneday = [NSDate date];
    unsigned int NSUInteger =   NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    NSDateComponents *d = [cal components:NSUInteger fromDate:oneday toDate:today options:0];
     self.string = [NSString stringWithFormat:@"%ld年%ld月%ld日%ld时%ld分%ld秒", [d year],[d month], [d day], [d hour], [d minute], [d second]];
       [self.homeview.timer setText:_string];
    
}

-(void)creatHomekindview
{
    Homekindview *view = [[Homekindview alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height * 23 / 60 , self.view.frame.size.width, self.view.frame.size.height * 5 / 12)];

    [view.boy addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [view.girlfriend addTarget:self action:@selector(buttonAction1:) forControlEvents:UIControlEventTouchUpInside];
    [view.girl addTarget:self action:@selector(buttonAction2:) forControlEvents:UIControlEventTouchUpInside];
    [view.FM addTarget:self action:@selector(buttonAction3:) forControlEvents:UIControlEventTouchUpInside];
    [view.worker addTarget:self action:@selector(buttonAction4:) forControlEvents:UIControlEventTouchUpInside];
    [view.Sutdent addTarget:self action:@selector(buttonAction5:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:view];
    [view release];
}

-(void)buttonAction :(id)sender
{
    HomeKindViewController *home = [[HomeKindViewController alloc]init];
    [self.navigationController pushViewController:home animated:YES];
    [home change:@"http://api.liwushuo.com/v1/channels/5/items?limit=12&offset=0"];
}
-(void)buttonAction1 :(id)sender
{
    HomeKindViewController *home = [[HomeKindViewController alloc]init];
    [self.navigationController pushViewController:home animated:YES];
    [home change:@"http://api.liwushuo.com/v1/channels/9/items?limit=12&offset=0"];

}
-(void)buttonAction2 :(id)sender
{
    HomeKindViewController *home = [[HomeKindViewController alloc]init];
    [self.navigationController pushViewController:home animated:YES];
    [home change:@"http://api.liwushuo.com/v1/channels/6/items?limit=12&offset=0"];

}
-(void)buttonAction3 :(id)sender
{
    HomeKindViewController *home = [[HomeKindViewController alloc]init];
    [self.navigationController pushViewController:home animated:YES];
    [home change:@"http://api.liwushuo.com/v1/channels/17/items?limit=12&offset=0"];

}
-(void)buttonAction4 :(id)sender
{
    HomeKindViewController *home = [[HomeKindViewController alloc]init];
    [self.navigationController pushViewController:home animated:YES];
    [home change:@"http://api.liwushuo.com/v1/channels/10/items?limit=12&offset=0"];

}
-(void)buttonAction5 :(id)sender
{
    HomeKindViewController *home = [[HomeKindViewController alloc]init];
    [self.navigationController pushViewController:home animated:YES];
    [home change:@"http://api.liwushuo.com/v1/channels/2/items?limit=12&offset=0"];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
